<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="生成配置"
    @ok="gen"
    width="1000px"
    @visible-change="handleVisibleChange"
  >
    <div class="pt-3px pr-3px">
      <BasicForm @register="registerForm" :model="modelRef"/>
    </div>
  </BasicModal>
</template>
<script lang="ts" setup>
import {ref, nextTick} from 'vue';
import {BasicModal, useModalInner} from '/@/components/Modal';
import {BasicForm, FormSchema, useForm } from '/@/components/Form/index';
import {handleDown} from "/@/api/development/generatorApi";

const schemas: FormSchema[] = [
  {
    field: 'dsName',
    component: 'Input',
    label: '数据源',
    colProps: {
      span: 12,
    },
    componentProps:{
      disabled :true
    },
    show:false,
  },
  {
    field: 'tableName',
    component: 'Input',
    label: '表名称',
    colProps: {
      span: 12,
    },
    componentProps:{
      disabled :true
    },
    defaultValue: '111',
  },
  {
    field: 'packageName',
    component: 'Input',
    label: '包名',
    colProps: {
      span: 12,
    },
    componentProps: {
      placeholder: '自定义placeholder',
    }
  },
  {
    label: '作者',
    field: 'author',
    component: 'Input',
    colProps: {
      span: 12,
    },
    componentProps: {
      placeholder: '自定义placeholder',
    }
  },
  {
    label: '模块',
    field: 'moduleName',
    component: 'Input',
    colProps: {
      span: 12,
    },
    componentProps: {
      placeholder: '自定义placeholder',
    }
  },
  {
    label: '表前缀',
    field: 'tablePrefix',
    component: 'Input',
    colProps: {
      span: 12,
    },
    componentProps: {
      placeholder: '自定义placeholder',
    }
  },
  {
    label: '注释',
    field: 'comments',
    component: 'Input',
    colProps: {
      span: 12,
    },
    componentProps: {
      placeholder: '自定义placeholder',
    }
  },
  {
    label: '前端风格',
    field: 'style',
    component: 'RadioGroup',
    colProps: {
      span: 24,
    },
    defaultValue: '0',
    required: true,
    componentProps: {
      options: [
        {
          label: 'vben封装',
          value: '0',
        }
      ],
    },
  },
];
const props = defineProps({
  genData: {type: Object},
});
const modelRef = ref({});
const [
  registerForm,
  {
    validate
    ,updateSchema
  },
] = useForm({
  labelWidth: 120,
  schemas,
  showActionButtonGroup: false,
  actionColOptions: {
    span: 24,
  },
});

const [register,{ closeModal }] = useModalInner((data) => {
  data && onDataReceive(data);
});



function onDataReceive(data) {
  if( data.tableName.indexOf("-") != -1 ){
    updateSchema([
      {
        field: 'tableName',
        component:'InputTextArea',
        colProps: {
          span: 24,
        },
      }
    ]);
  }

  // 方式1;
  // setFieldsValue({
  //   field2: data.data,
  //   field1: data.info,
  // });

  // // 方式2
  modelRef.value = {
    tableName: data.tableName
    ,dsName: data.dsName
  };

  // setProps({
  //   model:{ field2: data.data, field1: data.info }
  // })
}

function handleVisibleChange(v) {
  v && props.genData && nextTick(() => onDataReceive(props.genData));
}
async function gen() {
  try {
    const data = await validate();
    handleDown(data)
      .then((res) => {
        console.log(res)
        closeModal();
      }).catch((res) => {
        console.log(res)
      });
  } catch (error) {
    console.log('not passing', error);
  }


}


</script>
